#include <game.h>

interface::interface() : base("interface")
{
	inv = new string<inventaire*>;
}

interface::~interface()
{
	delete inv;
}

int interface::init(coeur *c,IGUIEnvironment *e,IXMLReader* xmlInterface,IXMLReader* xmlText)
{
	base::init(0,c);
	env = e;

	log(stringc("Interface xml loading ..."));
	
	while(xmlInterface && xmlInterface->read())
	{
		if (xmlInterface->getNodeType()==EXN_ELEMENT)
		{
			if (core::stringw("inventaire") == xmlInterface->getNodeName())
			{
				addInventaire(xmlInterface->getAttributeValueAsInt(L"id"),xmlInterface->getAttributeValue(L"image"),xmlInterface->getAttributeValueAsInt(L"v"),INVENTAIRE_POS(xmlInterface->getAttributeValueAsInt(L"pos")));
				log( stringc("Add inventaire : id:") + xmlInterface->getAttributeValueAsInt(L"v") + ", image:\"" + xmlInterface->getAttributeValue(L"image") + "\", v:" + xmlInterface->getAttributeValueAsInt(L"v") + ", Pos:" + xmlInterface->getAttributeValue(L"pos"));
					
			}
			else if (core::stringw("cursor") == xmlInterface->getNodeName())
			{
				curDefault = mycore->loadTexture(xmlInterface->getAttributeValue(L"default"));
				curTake = mycore->loadTexture(xmlInterface->getAttributeValue(L"take"));
				curLook = mycore->loadTexture(xmlInterface->getAttributeValue(L"look"));
				curAction = mycore->loadTexture(xmlInterface->getAttributeValue(L"action"));
				log( stringc("Load cursor image : default:\"") + xmlInterface->getAttributeValue(L"default") + ", take:\"" + xmlInterface->getAttributeValue(L"take") + ", look:\"" + xmlInterface->getAttributeValue(L"look") + ", action:\"" + xmlInterface->getAttributeValue(L"action") + "\"");
			}
			else if (core::stringw("font") == xmlInterface->getNodeName())
			{
				mainFont = env->getFont(stringc(xmlInterface->getAttributeValue(L"main")).c_str());
				log( stringc("Load font : main:\"") + xmlInterface->getAttributeValue(L"main") + "\"");
			}
			else if (core::stringw("sound") == xmlInterface->getNodeName())
			{
				soundFind = stringc(xmlInterface->getAttributeValue(L"find")).c_str();
				log( stringc("Load sound : find:\"") + xmlInterface->getAttributeValue(L"find") + "\"");
			}
		}
	}

	env->getSkin()->setFont(mainFont);
	log("Main font put on");
	image = env->addImage(curDefault,position2d<s32>(0,0));
	log("Cursor image added");
	txt = env->addStaticText(L"",rect<s32>(200,600,800,620));
	txt->setTextAlignment(EGUIA_CENTER,EGUIA_CENTER);
	log("Txt added");
	
	afficherInventaire(false);
	log("hide the inventaire");
	
	return 1;
}

int interface::update()
{
	setImagePosition(mycore->getCurPos()-position2d<s32>(20,20));
	if ( (mycore->getCurPos().X<100)||
			(mycore->getCurPos().Y<100)||
			(mycore->getCurPos().X > mycore->getDim().Width-100)||
			(mycore->getCurPos().Y > mycore->getDim().Height-100)){
			afficherInventaire(true);
		}else
		{
			afficherInventaire(false);
		}
	for (unsigned int i=0;i<inv->size();i++)
		(*inv)[i]->update();
	env->getRootGUIElement()->bringToFront(image);
	
	selectedInv = -1;
	selectedItem = -1;
	for (unsigned int i=0;i<inv->size();i++)
	{
		if ( (*inv)[i]->getImage()->isPointInside( mycore->getCurPos() ) )
		{
			selectedInv = i;
			selectedItem = (*inv)[selectedInv]->getNumItem(mycore->getCurPos());
			break;
		}
	}
	setImage(curDefault);
	if ( (mycore->getSelectedAction()) && !v)
	{
		if (mycore->getSelectedAction()->getAction()==ACTION_OBSERVER)
			setImage(curLook);
		else if (mycore->getSelectedAction()->getAction()==ACTION_PRENDRE)
			setImage(curTake);
		else if (mycore->getSelectedAction()->getAction()==ACTION_ANIMATE)
			setImage(curAction);
	}
	if ((selectedItem!=-1)&& (*inv)[selectedInv]->getItem(selectedItem))
		setImage(curTake);

	if (handle)
		setImage(handle->getTexture());

	if (time < mycore->getTime())
		txt->setText(L"");
	return 1;
}

int interface::close()
{
	log("Deleting inv");
	for (int i=inv->size()-1;i>-1;i--)
	{
		log(stringc("Deleting inventaire ") + (*inv)[i]->getId());
		(*inv)[i]->close();
	}
	base::close();
	return 1;
}

int interface::setImage(ITexture* tex)
{
	image->setImage(tex);
	return 1;
}

int interface::afficherInventaire(bool a)
{
	v=a;
	for (unsigned int i=0;i<inv->size();i++)
	{
		(*inv)[i]->afficher(a);
	}
	return 1;
}

int interface::addInventaire(int i,stringc path,int v,INVENTAIRE_POS p)
{
	inventaire* inve = new inventaire;
	position2d<int> pos = position2d<int>(0,0);
	switch (p)
	{
	case INVENTAIRE_POS_GAUCHE:
		pos = position2d<int>(0,0);
		break;
	case INVENTAIRE_POS_DROITE:
		pos = position2d<int>(924,0);
		break;
	case INVENTAIRE_POS_BAS:
		pos = position2d<int>(0,668);
		break;
	case INVENTAIRE_POS_HAUT:
		pos = position2d<int>(100,0);
		break;
	}
	inve->init(i,mycore,env,path,pos,v,p);
	inv->append(inve);
	log("Inventaire added");
	return 1;
}

int interface::setImagePosition(position2d<s32> pos)
{
	image->setRelativePosition(rect<s32>(pos,dimension2d<s32>(80,80)));
	return 1;
}

int interface::addItemInventaire(item *m)
{
	log(stringc("Item ") + m->getId() + " added");
	return (*inv)[selectedInv]->addItem(selectedItem,m);
}


bool interface::OnEvent(const SEvent& event)
{
	if (event.EventType!=EET_MOUSE_INPUT_EVENT)
		return false;

	switch (event.MouseInput.Event)
	{
	case EMIE_LMOUSE_PRESSED_DOWN:
		if (selectedItem!=-1)
		{
			handle = (*inv)[selectedInv]->getItem(selectedItem);
			(*inv)[selectedInv]->removeItem(selectedItem);
			pselectedInv=selectedInv;
			pselectedItem=selectedItem;
			log(stringc("Handle item ") + handle->getId() + " from inventaire " + selectedInv);
		}
		return true;
		break;

	case EMIE_LMOUSE_LEFT_UP:
		if (mycore->getSelectedAction())
		{
			if (mycore->getSelectedAction()->getAction()==ACTION_ANIMATE)
			{
				if ( (!handle && (mycore->getSelectedAction()->getCombi()==-1)) || ( (handle) && (mycore->getSelectedAction()->getCombi()==handle->getId())))
				{
					animateMesh* mesh = (animateMesh*) mycore->getSelectedAction();
					mycore->getActionMesh(mesh->getAnim())->getNode()->setFrameLoop(mesh->getBegin(),mesh->getEnd());
					log(stringc("Set animation of ") + mesh->getAnim() + " from mesh " + mesh->getId());
				}
			}
			else if ( !handle && (mycore->getSelectedAction()->getAction()==ACTION_PRENDRE))
			{
				if ((mycore->getSelectedAction()->getCombi()==-1) || ( (handle) && (mycore->getSelectedAction()->getCombi()==handle->getId())))
				{
					selectedInv = getFreePlace();
					selectedItem = (*inv)[selectedInv]->getFreePlace();
					log(stringc("Put item ") + ((prendreMesh*) mycore->getSelectedAction())->getItem() + " in default position : inventaire " + selectedInv + " at place " + selectedItem );				addItemInventaire(mycore->getItem(((prendreMesh*) mycore->getSelectedAction())->getItem()));
                                	mycore->getSelectedAction()->getNode()->setVisible(false);
				}
			}
		}

		if (!handle)
			return true;

		if (selectedItem!=-1)//If an image is pointed by cursor
		{
			item* it = (*inv)[selectedInv]->getItem(selectedItem);
			if (it)//If there is an item in the image
			{
				if ((handle->getCombi()!=-1)&&(handle->getCombi()==it->getId()))//If the item handle combine with the item selected
				{
					mycore->play(soundFind);
					(*inv)[selectedInv]->removeItem(selectedItem);
					log(stringc("Combinaison de") + handle->getId() + " et de " + handle->getCombi() + " qui donne : " + handle->getCombiResult());
					addItemInventaire( mycore->getItem( handle->getCombiResult() ));
				}
				else//Sinon ils ne coresspondent pas
				{
					speak(mycore->getCombinaison());
					selectedInv=pselectedInv;
					selectedItem=pselectedItem;
					log(stringc("Put handle in previous position : inventaire ") + selectedInv + " at place " + selectedItem );
					addItemInventaire(handle);
				}
			}else//sinon il y en a pas
			{
				log(stringc("Put handle in inventaire ") + selectedInv + " at place " + selectedItem );
				addItemInventaire(handle);
			}

		}else//sinon aucune image est pointé
		{
			selectedInv=pselectedInv;
			selectedItem=pselectedItem;
			log(stringc("Put handle in previous position : inventaire ") + selectedInv + " at place " + selectedItem );
			addItemInventaire(handle);	
		}
		handle=0;
		return true;
		break;

	case EMIE_RMOUSE_PRESSED_DOWN:
		if (selectedItem!=-1)
		{
			speak( mycore->getObservationItem( (*inv)[selectedInv]->getItem(selectedItem)->getId() ) );
			log(stringc("Look item ") + (*inv)[selectedInv]->getItem(selectedItem)->getId()  );
		}
		else if (mycore->getSelectedAction())
		{
			if ((mycore->getSelectedAction()->getAction()==ACTION_OBSERVER)||(mycore->getSelectedAction()->getAction()==ACTION_PRENDRE)||(mycore->getSelectedAction()->getAction()==ACTION_ANIMATE))
			{
				speak( mycore->getObservationMesh( mycore->getSelectedAction()->getId() ) );
				log(stringc("Look observationMesh ") + mycore->getSelectedAction()->getId() );
			}
		}
		else
			return true;
		return true;
		break;
	default:
		break;
	}
	return false;
}

int interface::getFreePlace()
{
	for (unsigned int i=0;i<inv->size();i++)
	{
		if ( (*inv)[i]->getFreePlace() != -1 )
			return i;
	}
	return -1;
}

int interface::speak(localItem* loc)
{
	txt->setText(loc->getText().c_str());
	mycore->play(loc->getPath().c_str());
	time = mycore->getTime() + 3000;
	return 1;
}


